package loaderlib.loader
{
	import flash.events.EventDispatcher;
	import flash.utils.getTimer;
	
	import loaderlib.agent.IRequester;

	/**
	 * the request hold every request.
	 * 
	 * when every item want load resource, the request, which get from same URI, link as a FIFO queue.
	 * they have the same RequestHeader to send real request to net.
	 * 
	 * @author TangZhen
	 * 
	 */
	dynamic public class Request extends EventDispatcher
	{
		
		/**
		 * the requestQueue is the queue link. every element has same url request will link in this queue;
		 * 
		 * @default null 
		 */
		public var requestQueue:Request = null;
		
		/**
		 * the tagQueue is the queue link. every element has same tag name will link in this queue;
		 * 
		 * @default null
		 */
		public var tagQueue:Request = null;
		
		/**
		 * url is the url request string. Future, i'll check it;
		 *  
		 * @default null
		 */
		internal var url:String;
		
		internal var dataFormat:String;
		
		/**
		 * _host is the host, that send the request.
		 * 
		 * @default null
		 */
		internal var _host:IRequester;
		
		/**
		 * startTime: the request start time
		 * 
		 * it's the internal from the swf start to the request initialization.
		 */
		private var startTime:int;
		
		/**
		 * stopTime: the request finish time
		 * 
		 * it's the internal from the swf start to the request get the full data.
		 */
		private var stopTime:int;
		
		public var requestHeader:RequestHeader;
		
		
		public function Request(host:IRequester)
		{
			super(null);
			this._host = host ;
			this.url = this._host.url;
			this.dataFormat = this._host.dataFormat;
			this["tagName"] = this._host.loaderTag;
			this.startTime = getTimer()
		}
		
		public function update():void
		{
			if(this.url != this._host.url)
			{
				this.url = this._host.url;
				this.startTime = getTimer();
				if(this.header)
				{
	
				}	
			}
		}
		
		/**
		 * finish is the callback function
		 * 
		 * when the real loader get the data, every request call the finish function.
		 * 
		 * @param data the received data;
		 * 
		 */
		public function finish(data:*):void
		{
/**
* 	if the host url aren't changed .
*/
			if(this._host.url == this.url)
			{
				this.stopTime = getTimer();
				this._host.getData(data);
			}
		}
		
		public function equal(item2:Request):Boolean
		{
			if(item2._host == this._host)
			{
				return true;
			}
			return false;
		}
		
	}
}